import {
    Alert,
    Toast
} from 'wuss-weapp';

let app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        auth: false,
        yardId: "",
        tabbar: {},
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        app.editTabbar();
        this.setData({
            theme: app.globalData.theme,
            yardId: app.globalData.yardId,
            tabbar: app.globalData.tabbar
        });
        app.setNavigationBarColor();
        this.indexAlternate();
        this.indexGetNoticeList();
        this.getRecommendRoundList();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    /**
     * 跳转通知列表
     */
    indexNoticeList: function (e) {
        let url = e.currentTarget.dataset.url;
        wx.navigateTo({
            url: '/pages/index/notice/notice_detail/notice_detail?url=' + url,
        })
    },
    /**
     * 投票
     */
    indexVote: function () {
        app.isVerifiedLogin("/pages/index/vote/vote");
    },
    /**
     * 获取通知公告
     */
    indexGetNoticeList: function () {
        app.wxGetRequest("/wechart/microofficial/getNoticeList", {
            area_code: app.globalData.parentAreaCode
        }).then(result => {
            let msgList = [];
            let data = result.data.data;
            data.forEach((item, index) => {
                msgList.push({
                    title: item.title
                });
            });
            this.setData({
                msgList: msgList,
            });
        });
    },
    /**
     * 获取生活服务
     */
    indexShops: function () {
        app.wxGetRequest("/app-community/v1/shops", {
            area_code: app.globalData.parentAreaCode
        }).then(result => {
            this.setData({
                shopList: result.data.records,
            });
        });
    },
    /**
     * 雪亮工程
     */
    indexVideo: function () {
        // app.isVerifiedAuth("/pages/index/video_surveillance/video_surveillance");
        wx.navigateTo({
            url: '/pages/index/video_surveillance/video_surveillance',
        })

    },
    /**
     * 意见信箱
     */
    indexOpinionBox: function () {
        app.isVerifiedLogin("/pages/mine/opinion_box/opinion_box");
    },
    /**
     * 商家详情
     */
    indexShopDetail: function (e) {
        let id = e.currentTarget.dataset.id;
        let phone = e.currentTarget.dataset.phone;
        let type = e.currentTarget.dataset.type;
        if (type == "D0041_15" || type == "D0041_16") {
            wx.navigateTo({
                url: '/pages/index/service_more/consultation/consultation?id=' + id
            })
            return
        }
        wx.navigateTo({
            url: '/pages/index/life_service/life_service_detail/life_service_detail?id=' + id + "&phone=" + phone
        })
    },
    /**
     * 轮播
     */
    indexAlternate: function () {
        app.wxGetRequest("/wechart/microofficial/getStreetList", {
            areaCode: app.globalData.parentAreaCode
        }).then(result => {
            console.log("执行了" + JSON.stringify(result))
            let data = result.data.data;
            let alternateList = [];
            data.forEach((item, index) => {
                if (index < 5) { //只显示五条
                    alternateList.push(item);
                }
            });
            if (alternateList.length == 0) {
                alternateList = [{
                    "first_picture": 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1566215345089&di=dfe6a7be37ae4c1e77e5ac66e82b2e10&imgtype=0&src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F7e12f858ce7d6b555b487c86f8a805a4c8dbb89eefc6-2yT1Ic_fw658'
                }, {
                    'first_picture': 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1566215452621&di=cdf0bafcd3c360196ce62702196e3d52&imgtype=0&src=http%3A%2F%2Fy2.ifengimg.com%2Fcmpp%2F2014%2F07%2F11%2F10%2F62b098ca-2c1f-4ba6-adb2-fc3c17a54cf3.jpg'
                }];
            }
            this.setData({
                alternateList: alternateList,
            });
        });
    },
    /**
     * 更多商家
     */
    indexMoreShops: function () {
        wx.navigateTo({
            url: '/pages/index/service_more/service_more',
        })
    },
    /**
     * 议事大厅
     */
    indexBusinessHall: function () {
        app.isVerifiedLogin("/pages/index/business_hall/business_hall");
    },
    /**
     * 更多
     */
    indexMoreFunction: function () {
        wx.navigateTo({
            url: '/pages/index/more_function/more_function',
        })
    },
    /**
     * 居民活动
     */
    indexResidentActivity: function () {
        app.isVerifiedLogin("/pages/index/resident_activity/resident_activity");
    },
    /**
     * 政务党务
     */
    indexGovernmentAffair: function () {
        app.wxGetRequest("/wechart/microofficial/getGovernmentTypeTree", {}).then(result => {
            let data = result.data.data;
            if (result.data.code == 1) {
                data.forEach((item, iundex) => {
                    if (item.name == "社区党建") {
                        wx.navigateTo({
                            url: '/pages/index/government_affairs/government_affairs?item=' + JSON.stringify(item),
                        })
                    }
                });
            }
        });
    },
    // 获取推荐商家
    getRecommendRoundList: function () {
        app.wxGetRequest("/wechart/microofficial/getRecommendRoundList", {
            area_code: app.globalData.parentAreaCode,
            // type: "D0041_3"
        }).then(result => {
            console.error(result)
            if (result.data.code == 1) {
                let list = result.data.data;
                this.setData({
                    shopList: list
                });
            }
        });
    },
    /**
     * 院坝消息
     */
    indexNotice: function () {
        app.isVerifiedLogin("/pages/index/notice/notice");
    },
    /**
     * 物业报修
     */
    indexpRopertyRepairs: function () {
        // app.isVerifiedAuth().then(()=>{
        //   if (this.data.yardId){
        //     wx.navigateTo({
        //       url: '/pages/index/property_repairs/property_repairs',
        //     })
        //   }
        // });
        if (app.globalData.userInfo["user_id"]) {
            app.getUserInfo().then(result => {
                app.globalData.userInfo = result.data.data;
                if (app.globalData.userInfo["people_id"]) {
                    app.isBindCommunity().then(result => {
                        result.data.data.list.forEach((item, index) => {
                            if (index == 0) {
                                app.globalData.roomId = item.room_id;
                                app.globalData.yardId = item.yard_id;
                                app.globalData.areaCode = item.area_code;
                                wx.setStorageSync('roomId', item.room_id);
                                wx.setStorageSync('yardId', item.yard_id);
                                wx.setStorageSync('areaCode', item.area_code);
                            }
                        });
                        if (result.data.data.list.length > 0) { //存在房屋
                            this.setData({
                                yardId: app.globalData.yardId
                            });
                            wx.navigateTo({
                                url: '/pages/index/property_repairs/property_repairs',
                            })
                        } else { //不存在房屋
                            app.alert('提示', '请使用本人信息到【我的->入住登记】进行第一次登记授权!');
                        }
                    });
                } else {
                    wx.navigateTo({
                        url: '/pages/index/authorization_certification/authorization_certification',
                    })
                }
            });
        } else {
            wx.navigateTo({
                url: '/pages/mine/login/login',
            })
        }
    },
    /**
     * 社区组织
     */
    indexCommunityOrganization: function () {
        app.isVerifiedLogin("/pages/index/community_organization/community_organization");
    },
    /**
     * 随手拍
     */
    indexEvent: function () {
        app.isVerifiedLogin("/pages/event/event_report/event_report_small_type/event_report_small_type");
    },
    /**
     * 抽奖
     */
    indexLottery: function () {
        app.isVerifiedLogin("/pages/index/lottery/lottery");
    },
    /**
     * 社区助手
     */
    indexCommunityAssistant: function () {
        app.isVerifiedLogin("/pages/index/community_assistant/community_assistant");
    },
    /**
     * 院坝风采
     */
    indexElegantDemeanour: function () {
        app.isVerifiedLogin("/pages/index/elegant_demeanour/elegant_demeanour");
    },

    indextoAppRYB: function () {
        wx.navigateToMiniProgram({
            appId: 'wx4b18951ae5c405cf',
            success(res) {
                // 打开成功
            }
        })
    },
    /**
     * 居家精品
     */
    indextoApplet: function () {
        wx.navigateToMiniProgram({
            appId: 'wx8b9aae2eb55d5354',
            success: res => {

            },
            fail: err => {

            }
        })
    },
    onSwiperTap: function (event) {
        var postid = event.target.dataset.postId
        console.log(postid + "执行了")
        wx.navigateTo({
            url: "../../pages/index/guide/guide?id=" + postid
        })
    }
})